To use VEE to interact with Access you need to use the ActiveX 
Automation methods.  This is a basic tutorial which shows you how 
to set that connection between your VEE applications and Access via 
these methods.

DISCLAIMER, WARRANTY AND COPYRIGHT
These examples are provided on an 'as is' basis with no explicit or
implied warranty.  Copyright is retained by the author but free use
including copying and use for business purposes is expressly permitted.

This explanation goes with a set of VEE files and an Access database
file, Sample db.mdb that you should also have - the VEE files are in
version 6.  Please contact Info@PreciselySo.Co.UK if you need these in
VEE 5 format.

The tutorial spans six vee files Access 2000 - 1 to 6, which are used 
to help you through the steps outlined below:

______________________________________


First, you need to know a little about object-oriented terminology:

There are objects you need to use 
the Database engine (application), this uses the DAO ( data access
  object ) library
databases, 
recordsets,
records
and fields.  

In that order, each is a member of the previous object and there can 
be many records in a recordset, for example.

Each object can have a name and you can refer to objects by name or by
reference.

Objects have properties ( values associated with them ) and methods
( functions which operate on them ) and these are accessed via the dot
notation.  As an illustration I may want to set a particular field type
to numeric...

A couple of prerequisites here - first I need to have a reference to the
recordset: say rst1 and I also need to have set the 'pointer' in the
recordset to the record I want e.g.

rst1.MoveFirst()        [sets the pointer to the first record in the
                        recordset - note that this is an operation or a
                        'method' and therefore, in VEE, you need to
                        remember to add () at the end!]

then I use

rst1.Fields(0).Type = dbNumeric

to set the type.

rst1.Fields(0) = "a text value"

sets the value of the field.  If the name of the field was 'field1' I
could use:

rst1.Fields('field1) = "a text value"             or even
rst1.Fields('field1).value = "a text value"       value is the 'default'
                                                  property so it is used
                                                  if no other property is
                                                  specified

Hmmm, so just what is a recordset?

Access doesn't let you deal directly with tables ( as far as I know ) so
you make a copy of the table into a thing called a recordset: you can
add, delete, edit as you like and then, to make the changes 'stick', the
method

rst1.Update()

is used.

If that's clear, I'll refer to an example, starting from scratch.

In a new VEE file, select the menu Device -> ActiveX Automation
References and select the Microsoft DAO 3.6 Object Library.  This gives
you access to the library from the VEE function and object browser.  If
you see a different version than 3.6 then use the latest version you
have and reflect the changes in the declarations accordingly.

We need to declare a couple of objects - well, at least one ( because we
can do the rest by reference ) but more likely I'd like to refer to both
the database and the recordset by name.  Names are really useful for
readability so use these to your advantage.

Sorry, but I have done loads in Access and I always use db for the
database and rst for the recordset ( if I only want to deal with one of
each ) - but I'm sure you can think of better names!

Declare two objects using Data -> Variable -> Declare Variable:

db database and
rst recordset

I've left the scope as Global, although I generall prefer to set this to
something smaller in scope if possible.  The Type is Object and I have
specifically set the object types ( my preference ), but you can leave
them unspecified

(Access 2000 - 1)

BTW, the reason I set everything I can is because it makes me think what
is expected.  VEE will throw up an error if I am wrong and then I learn
something; if not VEE will deal with the error here automatically but
may cause an unexpected output later...

There are also two 'set' lines where the objects are set to the
specific objects:

the first includes the database file name ( and path if required ) and
the second the table I want to view.

There are some options you might like to know about here, in the

Set db = createObject("DAO.DBEngine.36").OpenDatabase("Sample db.mdb", TRUE, TRUE)

line the first TRUE = exclusive use ( preventing others from opening the
databese while you are using it ) and the second is for 'read-only'.
This is nice and safe.  Later in this mini-tutorial you'll see I have to
change that to FALSE to allow me to make changes to the database.

Similarly in

Set rst = db.OpenRecordSet("tbList",dbOpenDynaset)

I normally use dbOpenDynaset where I want to make changes and
dbOpenSnapshot when I don't.

BTW, to find out what options are available in VEE, open the Function &
Object Browser, select ActiveX Objects and at the end of the Class:
window you'll see a set of 'Enum' icons.  In the case just mentioned,
click on RecordsetTypeEnum and you will see the list of available
options.  Some are obvious, some are not.  You can use the number
( e.g. 2 ) instead of the name dbOpenDynaset but generally I think it
better practice to use the latter.

At this stage, the database has been 'opened' or 'connected' whichever
concept seems more natural to you.

Next, I'd like to find the record where the field 'b' contains the word
'jane'.  And then I want to read all of the fields of that record.

I have used the FindFirst method and then the Fields[.value] property
( as mentioned above ) to find and access the fields.

(Access 2000 - 2)

Beware!  the syntax for these seek, find etc. methods can be a bit
pernickety and so you may want to try them in Access first ( using a
query )...

Well, that's all there is to it!  For tidyness you should release the
database and the memory for the objects.  Access 2000 - 3 shows how this
is done.  You can now run this program and, after it has finished, open
the Sample db by double-clicking on the file without getting messages
that the database is being opened 'read only' which you will get if you
do the same with Access 2000 - 2 before closing the VEE file.

Next I would like to add some records to the database.  Before doing
this you may like to take a copy of the database because VEE is
intending to change it!

There are several options but the two you might like to use at first
are:

rst.AddNew()            adds a new record to the dataset and
rst.Edit()              sets 'edit' mode on the current record ( i.e. in
                        our case the one with 'jane' in field 'b' )

Try running Access 2000 - 4 which adds a record.  Check that the record
has been added to the Sample db file.  No?

Remember what I said earlier, the dataset is a _copy_ of the table.  To
commit the changes I need to apply the Update method.

(Access 2000 - 5)

Now if you have a look at tbList in the database it has a new record.
Note also that because of the way the 'autonumber' field 'a' works, the
numbers are not necessarily sequential.  However, they will always be
unique.

Finally, in Access 2000 - 6, 500 records are added.  These need to be
added one-by-one and the Update method needs to be called each time
( unless someone knows differently! ).

Some useful Recordset methods
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
One of the examples above mentions a method FindFirst() and MoveFirst().

Other methods you may find useful

MoveLast()              move the record pointer to last record in the
                          recodset.  Do this before querying how many
                          records are in the recordset
MoveNext()              go to the next record
MovePrevious()          go to previous record


Michael J. Watts Consultant Jan 2002, Apr 2002

